package com.jiang.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.jiang.factory.ServiceFactory;
import com.jiang.service.IAreaService;
import com.jiang.service.impl.AreaServiceImpl;
import com.jiang.vo.Area;
import com.jiang.vo.AreaPlus;

@WebServlet(name="provinceServlet",urlPatterns="/provinceServlet/*")
public class ProvinceServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3933996960125189194L;

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml");
		// 这里主要是靠路径来区分调用的业务方法
		String uri = request.getRequestURI();
		String methodName = uri.substring(uri.lastIndexOf("/") + 1);
		try {
			Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
					HttpServletResponse.class);
			// 直接调用其方法，返回其结果
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	public void loadAllProvince(HttpServletRequest request, HttpServletResponse response){
		IAreaService areaService = ServiceFactory.getInstance(AreaServiceImpl.class);
		List<Area> areas = areaService.findAllArea();
		if(areas!=null){
			try {
				Document document = DocumentHelper.createDocument();
				Element root = document.addElement("provinces");
				for(Area area: areas){
					Element proEle = root.addElement("province");
					Element aidEle = proEle.addElement("aid");
					aidEle.setText(area.getAid().toString());
					Element titleEle = proEle.addElement("title");
					titleEle.setText(area.getTitle());
				}
				OutputFormat format = OutputFormat.createCompactFormat();
				format.setEncoding("UTF-8");
				XMLWriter xmlWriter = new XMLWriter(response.getWriter(), format);
				xmlWriter.write(document);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void loadAllCities(HttpServletRequest request, HttpServletResponse response){
		
		// 根据id选中要加载的数据，然后加载出城市来
		IAreaService areaService = ServiceFactory.getInstance(AreaServiceImpl.class);
		int areaId = Integer.parseInt(request.getParameter("aid"));
		List<AreaPlus> areaspluses = areaService.findAllAreaPlusByAreaId(areaId);
		if(areaspluses!=null){
			try {
				Document document = DocumentHelper.createDocument();
				Element root = document.addElement("cities");
				for(AreaPlus areaplus: areaspluses){
					Element cityEle = root.addElement("city");
					Element apidEle = cityEle.addElement("apid");
					apidEle.setText(areaplus.getApid().toString());
					Element aidEle = cityEle.addElement("aid");
					aidEle.setText(areaplus.getAid().toString());
					Element titleEle = cityEle.addElement("title");
					titleEle.setText(areaplus.getTitle());
				}
				OutputFormat format = OutputFormat.createCompactFormat();
				format.setEncoding("UTF-8");
				XMLWriter xmlWriter = new XMLWriter(response.getWriter(), format);
				xmlWriter.write(document);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
